﻿package igz.fleaxy.ui
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import igz.fleaxy.util.FilterType;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class LinkUi extends Sprite {

		protected var _Enable : Boolean;
		
		private var _Settings : Object;
		
		protected var _DisplayObject:DisplayObject;
		public function get $DisplayObject() : DisplayObject {
			return _DisplayObject;
		}
		public function set $DisplayObject( p_displayObject:DisplayObject ) : void {
			removeChild( _DisplayObject );
			
			_DisplayObject = p_displayObject;
			addChild( _DisplayObject );
		}

		/**
		 * Clase adaptadora que toma un DisplayObject y lo convierte en clickable.
		 * Añade una capa transparente y rectangular (que abarca toda la superficie del objecto)
		 * y por último añade los manejadores de eventos.
		 *
		 * @param	p_displayObject : (Sprite | Shape | TextField | Bitmap)
		 * @param	p_settings   : { 
		 * 				  "onClick" : null
		 *				, "onRollOver" : null
		 *				, "onRollOut" : null
		 *				, "onMouseDown" : null
		 * 				, "onMouseUp" : null
		 *			}
		 *
		 * @example
		 *
		 * var label:LabelUi = new LabelUi("Prueba", "", null);
		 * var link:LinkUi = new LinkUi(label,
		 * 								{ onClick : metodo1
		 * 								 , onRollOver : metodo2
		 * 								 , onRollOut : metodo3 }
		 * 								);
		 * addChild(link);
		 *
		 */
		public function LinkUi(p_displayObject:DisplayObject, p_settings:Object = null) {
			_Settings = { "onClick" : null
						, "onRollOver" : null
						, "onRollOut" : null
						, "onMouseOver": null
						, "onMouseOut": null
						, "onMouseDown" : null
						, "onMouseUp" : null
						}
			ObjectUtil.$Merge(p_settings, _Settings);
			
			_DisplayObject = p_displayObject;
			addChild( _DisplayObject );

			// Añade la capa transparente y rectangular sobre el objeto
			var shield:Shape = new Shape();
			shield.graphics.beginFill(0x000000, 0);
			shield.graphics.drawRect(0,0,p_displayObject.x+p_displayObject.width,p_displayObject.y+p_displayObject.height)
			shield.graphics.endFill();
			addChild( shield );

			//this.addEventListener(Event.ADDED_TO_STAGE, _OnAddedToStage);
			tabEnabled = false;
			
			$Enable();
		}

		//protected function _OnAddedToStage(p_event:Event):void {
			//_OnRollOut();
		//}

		public function $Enable() : void {
			
			_ApplyOptionsEnable();
			
			if ( _Settings["onClick"] != null ) {
				this.addEventListener(MouseEvent.CLICK, _Settings["onClick"]);
			}
			if ( _Settings["onMouseOver"] != null ) {
				this.addEventListener(MouseEvent.MOUSE_OVER, _Settings["onMouseOver"]);
			}
			if ( _Settings["onMouseOut"] != null ) {
				this.addEventListener(MouseEvent.MOUSE_OUT, _Settings["onMouseOut"]);
			}
			if ( _Settings["onRollOver"] != null ) {
				this.addEventListener(MouseEvent.ROLL_OVER, _Settings["onRollOver"]);
			}
			if ( _Settings["onRollOut"] != null ) {
				this.addEventListener(MouseEvent.ROLL_OUT, _Settings["onRollOut"]);
			}
			if ( _Settings["onMouseDown"] != null ) {
				this.addEventListener(MouseEvent.MOUSE_DOWN, _Settings["onMouseDown"]);
			}
			if ( _Settings["onMouseUp"] != null ) {
				this.addEventListener(MouseEvent.MOUSE_UP, _Settings["onMouseUp"]);
			}
		}
		
		protected function _ApplyOptionsEnable() : void {
			_Enable = true;
			this.buttonMode = true;
			this.useHandCursor = true;
			this.filters = []
		}
		
		public function $Disable() : void {
			
			_ApplyOptionsDisable();

			if ( _Settings["onClick"] != null ) {
				this.removeEventListener(MouseEvent.CLICK, _Settings["onClick"]);
			}
			if ( _Settings["onMouseOver"] != null ) {
				this.removeEventListener(MouseEvent.MOUSE_OVER, _Settings["onMouseOver"]);
			}
			if ( _Settings["onMouseOut"] != null ) {
				this.removeEventListener(MouseEvent.MOUSE_OUT, _Settings["onMouseOut"]);
			}
			if ( _Settings["onRollOver"] != null ) {
				this.removeEventListener(MouseEvent.ROLL_OVER, _Settings["onRollOver"]);
			}
			if ( _Settings["onRollOut"] != null ) {
				this.removeEventListener(MouseEvent.ROLL_OUT, _Settings["onRollOut"]);
			}
			if ( _Settings["onMouseDown"] != null ) {
				this.removeEventListener(MouseEvent.MOUSE_DOWN, _Settings["onMouseDown"]);
			}
			if ( _Settings["onMouseUp"] != null ) {
				this.removeEventListener(MouseEvent.MOUSE_UP, _Settings["onMouseUp"]);
			}
		}
		
		protected function _ApplyOptionsDisable() : void {
			_Enable = false;
			this.buttonMode = false;
			this.useHandCursor = false;
			this.filters = [ FilterType.$GRAYSCALE ];
		}
	}
	
}